package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.pojo.TbOrderItemExample;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.OrderItemService;
import com.dongdongshop.service.SellerService;
import com.dongdongshop.vo.MyOrderVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@DubboService
@Service
public class OrderItemServiceImpl implements OrderItemService {
    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;
    @DubboReference
    private SellerService tbSellerService;

    @Override
    public void addOrderItem(TbOrderItem tbOrderItem) {
        tbOrderItemMapper.insertSelective(tbOrderItem);
    }

    @Override
    public void updateStatusTo2(String out_biz_no) {
        tbOrderItemMapper.updateStatusTo2(out_biz_no);
    }

    @Override
    public void updateAllOrderItem(String out_trade_no, String trade_no) {
        tbOrderItemMapper.updateStatusByOrderNum(out_trade_no,trade_no);
    }

    @Override
    public List<MyOrderVo> selectInfoByOrder(List<TbOrder> orderList) {
        List<MyOrderVo> list = new ArrayList<>();

        for (TbOrder tbOrder : orderList) {
            MyOrderVo myOrderVo = new MyOrderVo();

            TbOrderItemExample example = new TbOrderItemExample();
            TbOrderItemExample.Criteria criteria = example.createCriteria();
            criteria.andOrderIdEqualTo(tbOrder.getOrderId());
            criteria.andStatusEqualTo("1");

            List<TbOrderItem> orderItemList = tbOrderItemMapper.selectByExample(example);
            if (orderItemList != null){
                for (TbOrderItem i: orderItemList) {
                    Long id = i.getId();
                    i.setSid(String.valueOf(id));
                }
                myOrderVo.setOrderItemList(orderItemList);

                TbSeller tbSeller = tbSellerService.selectSellerById(tbOrder.getSellerId());
                myOrderVo.setSellerId(tbSeller.getSellerId());
                myOrderVo.setSellerName(tbSeller.getName());
                myOrderVo.setOrderNum(tbOrder.getOrderNum());

                list.add(myOrderVo);
            }
        }
        return list;
    }
}
